{% extends 'dashboard.html' %}

{% block content %}


<div class="right_col" role="main">
    <div class="">
        <form id="demo-form2" data-parsley-validate class="form-horizontal form-label-left" method="post" action="{{ url_for('dev_work_update',id=work.id) }}">
            {{ form.hidden_tag() }}
        <div class="col-md-3 col-sm-3 col-xs-12">
                <div class="x_panel">
                  <div class="x_title">
                    <h2>新增工单</h2>
                    <div class="clearfix"></div>
                  </div>
                  <div class="x_content">
                    <br />

                      <div class="form-group">
                        <label class="control-label col-md-4 col-sm-4 col-xs-12" for="first-name">工单名<span class="required">*</span>
                        </label>
                          <input id="{{ form.name.name }}" name="{{ form.name.name }}" type="hidden" value="{{ work.name }}">
                        <div class="col-md-8 col-sm-8 col-xs-12">
                          {{ work.name }}
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="control-label col-md-4 col-sm-4 col-xs-12" for="last-name">数据库<span class="required">*</span>
                        </label>
                          <input id="{{ form.db_config.name }}" name="{{ form.db_config.name }}" type="hidden" value="{{ work.db_config }}">
                        <div class="col-md-8 col-sm-8 col-xs-12">
                          {{ work.db_config }}
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="control-label col-md-4 col-sm-4 col-xs-12">备份<span class="required">*</span></label>
                          <input id="{{ form.backup.name }}" name="{{ form.backup.name }}" type="hidden" value="{{ work.backup }}">
                        <div  class="col-md-8 col-sm-8 col-xs-12">
                          {% if work.backup == true %}是{% else %}否{% endif %}
                        </div>
                      </div>
                      <div class="form-group">
                        <label class="control-label col-md-4 col-sm-4 col-xs-12" for="last-name">审核人员<span class="required">*</span>
                        </label>
                          <input id="{{ form.audit.name }}" name="{{ form.audit.name }}" type="hidden" value="{{ work.audit }}">
                        <div class="col-md-8 col-sm-8 col-xs-12">
                          {{ work.audit }}
                        </div>
                      </div>


                      <div class="ln_solid"></div>
                      <div class="form-group">
                        <div class="col-md-12 col-sm-12 col-xs-12 col-md-offset-3">
                          <button class="btn btn-primary" type="button" onclick="{location.href='{{ url_for('dev_work') }}'}">取消</button>
                          <button type="submit" class="btn btn-success">提交</button>
                        </div>
                      </div>
                      <br>
                      <div class="form-group">
                        <div class="col-md-12 col-sm-12 col-xs-12 col-md-offset-3">
                            {% with messages = get_flashed_messages() %}
                            {% if messages %}
                                 <ul>
                                    {% for message in messages %}
                                        <li><font color='#FF0000'>{{ message }}</font></li>
                                    {% endfor %}
                                 </ul>
                            {% endif %}
                            {% endwith %}
                        </div>
                      </div>


                  </div>
                </div>
        </div>
        <div id="last" class="col-md-9 col-sm-9 col-xs-12">
            <div class="x_panel">
                <div class="x_content">
                      <div class="form-group">
                        <div class="col-md-12 col-sm-12 col-xs-12 ">
                           <label for="message">SQL内容:</label>
                            <textarea id="{{ form.sql_content.name }}" name="{{ form.sql_content.name }}" required="required" class="form-control" name="message" data-parsley-trigger="keyup" placeholder="请输入类似sql： use test; create table t1( id int);"
                            data-parsley-validation-threshold="50" rows="20">{{ work.sql_content }}</textarea>
                            <br>
                            <button id="slqcheck" type="button" class="btn btn-info" onclick="sqlcheck()">检查SQL</button>
                         </div>
                       </div>
                </div>
            </div>
        </div>
        </form>
    </div>
 </div>

<script src="{{url_for('static',filename='vendors/jquery/dist/jquery.min.js')}}"></script>
<script>
 function sqlcheck() {
	var result = true;
	var sqlContent= $("#{{ form.sql_content.name }}").val();
	var dbConfig = $("#{{ form.db_config.name }}").val();
	if (sqlContent === null || sqlContent.trim() === "" || sqlContent == $("#{{ form.sql_content.name }}").attr("placeholder")) {
		alert("SQL内容不能为空！");
        return result = false;
	} else if (dbConfig === null || dbConfig == $("#{{ form.db_config.name }}").attr("data-placeholder")) {
		alert("请选择要上线的数据库！");
        return result = false;
	}
	if (result === true) {
        $.ajax({
		type: "post",
		url: "{{ url_for('dev_work_check') }}",
		dataType: "json",
		data: {
			'sqlContent': sqlContent,
			'dbConfig': dbConfig
		},
		success: function (data) {
           if (data.status == 0) {
               var result = data.data;
               var finalHtml ="<div id='result' class='col-md-12 col-sm-12 col-xs-12'><div class='x_panel'><div class='x_content'><table class='table'><thead><tr><th>sql语句</th><th>自动审核结果</th></tr></thead><tbody>";
               for (var i=0; i<result.length; i++) {
                   if (result[i][4] != "None") {
                       finalHtml+="<tr><td><font color='#FF0000'>"+result[i][5]+"</font></td><td><font color='#FF0000'>"+result[i][4]+"</font></td></tr>"
                   } else {
                       finalHtml+="<tr><td><font color='#00A600'>"+result[i][5]+"</font></td><td><font color='#00A600'>"+result[i][4]+"</font></td></tr>"
                   }
               }
               finalHtml+="</tbody></table></div></div></div>";
               $('#result') .remove();
               $('#last').append(finalHtml);
           } else  {
               alert(data.msg);
           }

	        }

        })
    }
 }





</script>
{% endblock %}